6. 折れ線グラフ

6.1. 概要

6.2. Plotlyによる作図方法

6.3. MADB Labを用いた作図例

import pandas as pd
import plotly.express as px

import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/episodes.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
# 連載週数の最小値
MIN_WEEKS = 5
def show_fig(fig):
    """Jupyter Bookでも表示可能なようRendererを指定"""
    fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    fig.show(renderer=RENDERER)
df = pd.read_csv(PATH_DATA)

6.4. 作品別の掲載位置

N_CNAMES = 5
mcnames = sorted(df['mcname'].unique())
for mcname in mcnames:
    df_tmp = df[df['mcname']==mcname].reset_index(drop=True)
    df_cname = \
        df_tmp.value_counts('cname').reset_index(name='weeks')
    df_cname = \
        df_cname.sort_values(
            'weeks', ascending=False, ignore_index=True)
    cnames = df_cname['cname'][:N_CNAME].values
    df_plot = df_tmp[df_tmp['cname'].isin(cnames)].\
        reset_index(drop=True)
    fig = px.line(
        df_plot, x='datePublished', y='pageStartPosition',
        color='cname', title=f'{mcname}の長期連載作品')
    show_fig(fig)
cnames
array(['はじめの一歩', 'あひるの空', 'コータローまかりとおる!', 'FAIRY TAIL', '金田一少年の事件簿'],
      dtype=object)
df_cname
cname weeks
0 はじめの一歩 1184
1 あひるの空 571
2 コータローまかりとおる! 558
3 FAIRY TAIL 546
4 金田一少年の事件簿 513
... ... ...
1556 なんぱ人 亀丸 1
1557 にたり留吉 1
1558 に~らめっこしまショ!! 1
1559 ねこたん 1
1560 ~HWGP~ 東村山ウエスト・ゲート・パーク 1

1561 rows × 2 columns

df_tmp = \
    df.value_counts('cname').reset_index(name='weeks')
cnames = df_tmp['cname'][:5].values
df_plot = \
    df[df['cname'].isin(cnames)].reset_index(drop=True)
df_plot['datePublished'] = pd.to_datetime(
    df_plot['datePublished'])
fig = px.line(
    df_plot, x='datePublished', y='pageStartPosition',
    color='cname')
show_fig(fig)